package com.itheima.studenthomework.controller;

import com.itheima.studenthomework.domain.Student;
import com.itheima.studenthomework.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentController {
    public void start() {
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("=========欢迎来到老师管理系统=========");
            System.out.println("1.查看信息");
            System.out.println("2.修改信息");
            System.out.println("5.返回上一级");
            System.out.println("6.退出");
            String str = sc.next();

            switch (str){
                case "1":
                    findStudent();
                    break;
                case "2":
                    updateStudent();
                    break;
                case "3":
                    break loop;
                case "4":
                    System.exit(0);
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }

    private void updateStudent() {
        Scanner sc = new Scanner(System.in);

        StudentService studentService = new StudentService();
        System.out.println("请输入您的学号：");
        String id = sc.next();
        int index = studentService.isExist(id);

        if(index == -1){
            System.out.println("学号信息不存在");
            return;
        }
        System.out.println("请输入您的姓名：");
        String name = sc.next();
        System.out.println("请输入您的年龄：");
        String age = sc.next();
        System.out.println("请输入您的生日：");
        String birthday = sc.next();

        Student stu = new Student(id,name,age,birthday);
        boolean flag = studentService.updateStudent(stu);

        if(flag){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    private void findStudent() {
        Scanner sc = new Scanner(System.in);

        StudentService studentService = new StudentService();
        /*
        通过学号查找信息：
        1.存在：放行
        2.不存在：提示信息并返回
         */
        System.out.println("请输入学生id：");
        String id = sc.next();
        int index = studentService.isExist(id);

        if(index == -1){
            System.out.println("学生信息不存在");
            return;
        }
        /*
        输出查询到的信息
         */
        Student student = studentService.findOneStudent(index);

        System.out.println("学号\t姓名\t年龄\t生日");
        System.out.println(student.getId()
                +"\t"+student.getName()
                +"\t"+student.getAge()
                +"\t"+student.getBirthday());
    }
}
